package top.yueyazhui.vhr.util;

import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;
import top.yueyazhui.vhr.model.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class POIUtil {

    public static ResponseEntity<byte[]> exportEmployee(List<Employee> list) {
        // 1. 创建一个 Excel 文档
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 2. 创建文档摘要
        workbook.createInformationProperties();
        // 3. 获取并配置文档摘要信息
        DocumentSummaryInformation documentSummaryInformation = workbook.getDocumentSummaryInformation();
        // 文档类别
        documentSummaryInformation.setCategory("员工信息");
        // 文档管理员
        documentSummaryInformation.setManager("yueyazhui");
        // 文档公司信息
        documentSummaryInformation.setCompany("www.yueyazhui.top");
        // 4. 获取并配置摘要信息
        SummaryInformation summaryInformation = workbook.getSummaryInformation();
        // 标题
        summaryInformation.setTitle("员工信息表");
        // 作者
        summaryInformation.setAuthor("yueyazhui");
        // 备注
        summaryInformation.setComments("本文档由 yueyazhui 提供");
        // 5. 创建样式
        // 创建标题行的样式
        HSSFCellStyle titleRowStyle = workbook.createCellStyle();
        titleRowStyle.setFillBackgroundColor(IndexedColors.GREY_25_PERCENT.index);
        titleRowStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
        titleRowStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        // 创建日期格式
        HSSFCellStyle dateCellStyle = workbook.createCellStyle();
        dateCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
        // 6. 创建表单
        HSSFSheet sheet = workbook.createSheet("员工信息表");
        // 设置列宽
        sheet.setColumnWidth(0, 6 * 256);
        sheet.setColumnWidth(1, 12 * 256);
        sheet.setColumnWidth(2, 10 * 256);
        sheet.setColumnWidth(3, 6 * 256);
        sheet.setColumnWidth(4, 12 * 256);
        sheet.setColumnWidth(5, 20 * 256);
        sheet.setColumnWidth(6, 8 * 256);
        sheet.setColumnWidth(7, 10 * 256);
        sheet.setColumnWidth(8, 12 * 256);
        sheet.setColumnWidth(9, 14 * 256);
        sheet.setColumnWidth(10, 22 * 256);
        sheet.setColumnWidth(11, 12 * 256);
        sheet.setColumnWidth(12, 50 * 256);
        sheet.setColumnWidth(13, 16 * 256);
        sheet.setColumnWidth(14, 16 * 256);
        sheet.setColumnWidth(15, 16 * 256);
        sheet.setColumnWidth(16, 10 * 256);
        sheet.setColumnWidth(17, 12 * 256);
        sheet.setColumnWidth(18, 12 * 256);
        sheet.setColumnWidth(19, 12 * 256);
        sheet.setColumnWidth(20, 12 * 256);
        sheet.setColumnWidth(21, 9 * 256);
        sheet.setColumnWidth(22, 9 * 256);
        sheet.setColumnWidth(23, 20 * 256);
        sheet.setColumnWidth(24, 20 * 256);
        // 7. 创建标题行
        HSSFRow r0 = sheet.createRow(0);
        // 8. 创建列
        HSSFCell c0 = r0.createCell(0);
        c0.setCellValue("编号");
        c0.setCellStyle(titleRowStyle);
        HSSFCell c1 = r0.createCell(1);
        c1.setCellValue("姓名");
        c1.setCellStyle(titleRowStyle);
        HSSFCell c2 = r0.createCell(2);
        c2.setCellValue("工号");
        c2.setCellStyle(titleRowStyle);
        HSSFCell c3 = r0.createCell(3);
        c3.setCellValue("性别");
        c3.setCellStyle(titleRowStyle);
        HSSFCell c4 = r0.createCell(4);
        c4.setCellValue("出生日期");
        c4.setCellStyle(titleRowStyle);
        HSSFCell c5 = r0.createCell(5);
        c5.setCellValue("身份证号码");
        c5.setCellStyle(titleRowStyle);
        HSSFCell c6 = r0.createCell(6);
        c6.setCellValue("婚姻状况");
        c6.setCellStyle(titleRowStyle);
        HSSFCell c7 = r0.createCell(7);
        c7.setCellValue("民族");
        c7.setCellStyle(titleRowStyle);
        HSSFCell c8 = r0.createCell(8);
        c8.setCellValue("籍贯");
        c8.setCellStyle(titleRowStyle);
        HSSFCell c9 = r0.createCell(9);
        c9.setCellValue("政治面貌");
        c9.setCellStyle(titleRowStyle);
        HSSFCell c10 = r0.createCell(10);
        c10.setCellValue("电子邮件");
        c10.setCellStyle(titleRowStyle);
        HSSFCell c11 = r0.createCell(11);
        c11.setCellValue("电话号码");
        c11.setCellStyle(titleRowStyle);
        HSSFCell c12 = r0.createCell(12);
        c12.setCellValue("联系地址");
        c12.setCellStyle(titleRowStyle);
        HSSFCell c13 = r0.createCell(13);
        c13.setCellValue("所属部门");
        c13.setCellStyle(titleRowStyle);
        HSSFCell c14 = r0.createCell(14);
        c14.setCellValue("职位");
        c14.setCellStyle(titleRowStyle);
        HSSFCell c15 = r0.createCell(15);
        c15.setCellValue("职称");
        c15.setCellStyle(titleRowStyle);
        HSSFCell c16 = r0.createCell(16);
        c16.setCellValue("聘用形式");
        c16.setCellStyle(titleRowStyle);
        HSSFCell c17 = r0.createCell(17);
        c17.setCellValue("入职日期");
        c17.setCellStyle(titleRowStyle);
        HSSFCell c18 = r0.createCell(18);
        c18.setCellValue("转正日期");
        c18.setCellStyle(titleRowStyle);
        HSSFCell c19 = r0.createCell(19);
        c19.setCellValue("合同起始日期");
        c19.setCellStyle(titleRowStyle);
        HSSFCell c20 = r0.createCell(20);
        c20.setCellValue("合同截止日期");
        c20.setCellStyle(titleRowStyle);
        HSSFCell c21 = r0.createCell(21);
        c21.setCellValue("合同期限/年");
        c21.setCellStyle(titleRowStyle);
        HSSFCell c22 = r0.createCell(22);
        c22.setCellValue("最高学历");
        c22.setCellStyle(titleRowStyle);
        HSSFCell c23 = r0.createCell(23);
        c23.setCellValue("毕业院校");
        c23.setCellStyle(titleRowStyle);
        HSSFCell c24 = r0.createCell(24);
        c24.setCellValue("专业");
        c24.setCellStyle(titleRowStyle);
        // 遍历数据
        for (int i = 0; i < list.size(); i++) {
            Employee employee = list.get(i);
            HSSFRow row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(employee.getId());
            row.createCell(1).setCellValue(employee.getName());
            row.createCell(2).setCellValue(employee.getWorkId());
            row.createCell(3).setCellValue(employee.getGender());
            HSSFCell cell4 = row.createCell(4);
            cell4.setCellStyle(dateCellStyle);
            cell4.setCellValue(employee.getBirthday());
            row.createCell(5).setCellValue(employee.getIdCard());
            row.createCell(6).setCellValue(employee.getWedlock());
            row.createCell(7).setCellValue(employee.getNation().getName());
            row.createCell(8).setCellValue(employee.getNativePlace());
            row.createCell(9).setCellValue(employee.getPoliticsstatus().getName());
            row.createCell(10).setCellValue(employee.getEmail());
            row.createCell(11).setCellValue(employee.getPhone());
            row.createCell(12).setCellValue(employee.getAddress());
            row.createCell(13).setCellValue(employee.getDepartment().getName());
            row.createCell(14).setCellValue(employee.getPosition().getName());
            row.createCell(15).setCellValue(employee.getJobLevel().getName());
            row.createCell(16).setCellValue(employee.getEngageForm());
            HSSFCell cell17 = row.createCell(17);
            cell17.setCellStyle(dateCellStyle);
            cell17.setCellValue(employee.getBeginDate());
            HSSFCell cell18 = row.createCell(18);
            cell18.setCellStyle(dateCellStyle);
            cell18.setCellValue(employee.getConversionTime());
            HSSFCell cell19 = row.createCell(19);
            cell19.setCellStyle(dateCellStyle);
            cell19.setCellValue(employee.getBeginContract());
            HSSFCell cell20 = row.createCell(20);
            cell20.setCellStyle(dateCellStyle);
            cell20.setCellValue(employee.getEndContract());
            row.createCell(21).setCellValue(employee.getContractTerm());
            row.createCell(22).setCellValue(employee.getTiptopDegree());
            row.createCell(23).setCellValue(employee.getSchool());
            row.createCell(24).setCellValue(employee.getSpecialty());
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        HttpHeaders headers = new HttpHeaders();

        try {
            headers.setContentDispositionFormData("attachment", new String("员工信息表.xls".getBytes("UTF-8"), "ISO-8859-1"));
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            workbook.write(baos);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new ResponseEntity<byte[]>(baos.toByteArray(), headers, HttpStatus.CREATED);
    }

    public static List<Employee> importEmployee(MultipartFile file, List<Nation> allNation, List<Politicsstatus> allPoliticsstatus, List<Department> allDepartment, List<Position> allPosition, List<JobLevel> allJobLevel) {
        List<Employee> list = new ArrayList<>();
        Employee employee = null;
        try {
            // 1. 创建一个 workbook 对象
            HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
            // 2. 获取 workbook 中表单的数量
            int numberOfSheets = workbook.getNumberOfSheets();
            for (int i = 0; i < numberOfSheets; i++) {
                // 3. 获取表单
                HSSFSheet sheet = workbook.getSheetAt(i);
                // 4. 获取表单中的行数
                int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
                for (int j = 0; j < physicalNumberOfRows; j++) {
                    // 5. 跳过标题行
                    if (j == 0) {
                        continue;
                    }
                    // 6. 获取行
                    HSSFRow row = sheet.getRow(j);
                    // 7. 防止数据中间有空行
                    if (row == null) {
                        continue;
                    }
                    // 8. 获取列
                    int physicalNumberOfCells = row.getPhysicalNumberOfCells();
                    employee = new Employee();
                    for (int k = 0; k < physicalNumberOfCells; k++) {
                        HSSFCell cell = row.getCell(k);
                        switch (cell.getCellType()) {
                            case STRING:
                                String stringCellValue = cell.getStringCellValue();
                                switch (k) {
                                    case 1:
                                        employee.setName(stringCellValue);
                                        break;
                                    case 2:
                                        employee.setWorkId(stringCellValue);
                                        break;
                                    case 3:
                                        employee.setGender(stringCellValue);
                                        break;
                                    case 5:
                                        employee.setIdCard(stringCellValue);
                                        break;
                                    case 6:
                                        employee.setWedlock(stringCellValue);
                                        break;
                                    case 7:
                                        int nationIndex = allNation.indexOf(new Nation(stringCellValue));
                                        employee.setNationId(allNation.get(nationIndex).getId());
                                        break;
                                    case 8:
                                        employee.setNativePlace(stringCellValue);
                                        break;
                                    case 9:
                                        int politicsstatusIndex = allPoliticsstatus.indexOf(new Politicsstatus(stringCellValue));
                                        employee.setPoliticId(allPoliticsstatus.get(politicsstatusIndex).getId());
                                        break;
                                    case 10:
                                        employee.setEmail(stringCellValue);
                                        break;
                                    case 11:
                                        employee.setPhone(stringCellValue);
                                        break;
                                    case 12:
                                        employee.setAddress(stringCellValue);
                                        break;
                                    case 13:
                                        int departmentIndex = allDepartment.indexOf(new Department(stringCellValue));
                                        employee.setDepartmentId(allDepartment.get(departmentIndex).getId());
                                        break;
                                    case 14:
                                        int positionIndex = allPosition.indexOf(new Position(stringCellValue));
                                        employee.setPosId(allPosition.get(positionIndex).getId());
                                        break;
                                    case 15:
                                        int jobLevelIndex = allJobLevel.indexOf(new JobLevel(stringCellValue));
                                        employee.setJobLevelId(allJobLevel.get(jobLevelIndex).getId());
                                        break;
                                    case 16:
                                        employee.setEngageForm(stringCellValue);
                                        break;
                                    case 22:
                                        employee.setTiptopDegree(stringCellValue);
                                        break;
                                    case 23:
                                        employee.setSchool(stringCellValue);
                                        break;
                                    case 24:
                                        employee.setSpecialty(stringCellValue);
                                        break;
                                }
                                break;
                            default: {
                                switch (k) {
                                    case 4:
                                        employee.setBirthday(cell.getDateCellValue());
                                        break;
                                    case 17:
                                        employee.setBeginDate(cell.getDateCellValue());
                                        break;
                                    case 18:
                                        employee.setConversionTime(cell.getDateCellValue());
                                        break;
                                    case 19:
                                        employee.setBeginContract(cell.getDateCellValue());
                                        break;
                                    case 20:
                                        employee.setEndContract(cell.getDateCellValue());
                                        break;
                                    case 21:
                                        employee.setContractTerm(cell.getNumericCellValue());
                                        break;
                                }
                                break;
                            }
                        }
                    }
                    list.add(employee);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }
}
